//
//  AccounSafeViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/8/29.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "AccounSafeViewController.h"
#import "RentVieitor.h"
#import "PhoneVerifyViewController.h"
#import "EditPassworldViewController.h"
#import "EditPhoneViewController.h"
#import "EditEmailViewController.h"

@interface AccounSafeViewController () {
    NSDictionary *info;
}

@end

@implementation AccounSafeViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"账号安全";
    self.view.backgroundColor = BackgroundColor;
    [self creatLeftView];
    [self creatView];
    [self loadData];
}

- (void)creatLeftView {
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 10,15 );
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}

//请求数据
- (void)loadData {
    
    NSDictionary *dataDic = User_listsValue;
    NSString *url = [NSString stringWithFormat:@"%@member/info/show",Main_Url];
    NSString *number = dataDic[@"number"];
    NSString *token = dataDic[@"token"];
    NSString *md5String = [NSString stringWithFormat:@"%@%@%@%@",number,token,APPKEY_URL,url];
    //32位小写
    md5String = [LCMD5Tool MD5ForLower32Bate:md5String];
    NSDictionary *param = @{
                            @"number":number,
                            @"token":token,
                            @"sign":md5String
                            };
    [AFHttpTool requestWihtUrl:url params:param success:^(id response) {
        
        
        if ([response[@"code"] isEqualToString:@"10000"]) {
            
            NSDictionary *data = response[@"data"];
            data = [Utils nullDic:data];
            [self reloadData:data];
        }
        
        
    } failure:^(NSError *err) {
        
        
    }];
}

//刷新数据
- (void)reloadData:(NSDictionary *)dataDic {
    
    info = [dataDic copy];
    UIView *nicknameView = (UIView *)[self.view viewWithTag:100];
    UILabel *nicknameLabel = (UILabel *)[nicknameView viewWithTag:200];
    nicknameLabel.text = @"***";
    
    UIView *mobileView = (UIView *)[self.view viewWithTag:101];
    UILabel *mobileLabel = (UILabel *)[mobileView viewWithTag:201];
    mobileLabel.text = dataDic[@"mobile"];
    
    UIView *emailView = (UIView *)[self.view viewWithTag:102];
    UILabel *emailLabel = (UILabel *)[emailView viewWithTag:202];
    emailLabel.text = dataDic[@"email"];
    
    
    
    
}


- (void)creatView {
    
    NSArray *textArray = @[@"登录密码",@"手机认证",@"邮箱认证"];

    //选项视图
    CGFloat heigt = 50;
    for (int i=0; i<textArray.count; i++) {
        
        UIView *selectView = [[UIView alloc] init];
        selectView.backgroundColor = [UIColor whiteColor];
        selectView.tag = 100+i;
        selectView.frame = CGRectMake(0,10+heigt*i , KWidth, heigt);
        [self.view addSubview:selectView];
        
        //添加手势
        UITapGestureRecognizer * PrivateLetterTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAvatarView:)];
        PrivateLetterTap.numberOfTouchesRequired = 1; //手指数
        PrivateLetterTap.numberOfTapsRequired = 1; //tap次数
        [selectView addGestureRecognizer:PrivateLetterTap];
        
        UILabel *selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, KWidth/2, 30)];
        selectLabel.text = textArray[i];
        selectLabel.font = [UIFont systemFontOfSize:15];
        selectLabel.textAlignment = NSTextAlignmentLeft;
        selectLabel.textColor = [Utils colorConvertFromString:@"#101010"];
        [selectView addSubview:selectLabel];
        
        
        UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        selectButton.frame = CGRectMake(KWidth-20, 20, 5, 10);
        [selectButton setBackgroundImage:[UIImage imageNamed:@"箭头-右"] forState:UIControlStateNormal];
        [selectView addSubview:selectButton];
        
        
        UILabel *phoneLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(KWidth/2, 10, KWidth/2-40, 30)];
       
        phoneLabel1.tag = 200+i;
        phoneLabel1.font = [UIFont systemFontOfSize:13];
        phoneLabel1.textAlignment = NSTextAlignmentRight;
        phoneLabel1.textColor = [Utils colorConvertFromString:@"#666666"];
        [selectView addSubview:phoneLabel1];
    
        //分割线
        CALayer *MlineLayer5 = [CALayer layer];
        MlineLayer5.backgroundColor = [[Utils colorConvertFromString:@"#cfcfcf"] CGColor];
        MlineLayer5.frame = CGRectMake(20 ,heigt-1, KWidth,10);
        MlineLayer5.backgroundColor = [[Utils colorConvertFromString:@"#F0F0F0"] CGColor];
        [selectView.layer addSublayer:MlineLayer5];
    
}
}

- (void)tapAvatarView:(UIGestureRecognizer *)recognizer {
    
    if (recognizer.view.tag == 100) {
        
        //修改密码
        EditPassworldViewController *phoneVc = [[EditPassworldViewController alloc] init];
        phoneVc.phone = info[@"mobile"];
        [self.navigationController pushViewController:phoneVc animated:YES];
        
    }else if (recognizer.view.tag == 101){
        
        //手机认证
        EditPhoneViewController *phoneVc = [[EditPhoneViewController alloc] init];
        phoneVc.phone = info[@"mobile"];
        [self.navigationController pushViewController:phoneVc animated:YES];
        
    }else if (recognizer.view.tag == 102){
        
        //邮箱认证
        EditEmailViewController *email = [[EditEmailViewController alloc] init];
        email.email = info[@"email"];
        email.phone = info[@"mobile"];
        [self.navigationController pushViewController:email animated:YES];
    }
}

- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
